#include "../_Module/CKDNS.h"
#include "CKDatabase.h"



namespace K_Engine
{
	CKDBSelectResult::CKDBSelectResult(MYSQL_RES* _res)
	{
		MYSQL_ROW _row;
		std::vector<std::string>* temp;
		MYSQL_FIELD* _field = mysql_fetch_fields(_res);
		nListLength = mysql_num_fields(_res);
		nResultLength = 0;
		
		for(int i = 0 ; i < nListLength ; i++)
		{
			listname.insert(
				std::map<std::string, int>::value_type(_field[i].name, i));
		}
		while((_row = mysql_fetch_row(_res)))
		{
			temp = new std::vector<std::string>();
			for(int i = 0 ; i < nListLength ; i ++)
			{
				temp->push_back(_row[i]);
			}
			listvalue.insert(
				std::map<int, std::vector<std::string>*>::value_type(
					nResultLength, temp));
			nResultLength++;
		}
	}
	
	
	CKDBSelectResult::~CKDBSelectResult()
	{
		for(int i = 0 ; i < nResultLength ; i++)
		{
			delete listvalue[i];
		}
	}
	
	
	const std::vector<std::string>* CKDBSelectResult::GetLine(const int index)
	{
		return listvalue[index];
	}
	
	
	const std::string& CKDBSelectResult::GetValue(const int index, std::string strListName)
	{
		// std::vector<std::string>* line = listvalue[index];
		// int pos = listname[strListName];
		// return (*line)[pos];
		return (*listvalue[index])[listname[strListName]];
	}
	
	
	int CKDBSelectResult::nGetResultLength() const
	{
		return nResultLength;
	}
	
	
	int CKDBSelectResult::nGetResultListLength() const
	{
		return nListLength;
	}
	
	
	//--------------------------------------------
	
	
	CKDatabase::CKDatabase() :
		bConnection(false)
	{
		mysql_init(&m_conn);
	}
	
	
	CKDatabase::~CKDatabase()
	{
		mysql_close(&m_conn);
	}
	
	
	void CKDatabase::SetHost(std::string _host)
	{
		m_dbhost = _host;
	}
	
	
	void CKDatabase::SetUserPasswork(std::string _user, std::string _passwork)
	{
		m_user = _user;
		m_passwork = _passwork;
	}
	
	
	void CKDatabase::SetDBname(std::string _dbname)
	{
		m_dbname = _dbname;
	}
	
	
	bool CKDatabase::connectDB()
	{
		if( "" == m_dbhost)
		{
			//若为空
			//则默认为本地
			m_dbhost = "127.0.0.1";
		}
		std::string host = GetHostByName(m_dbhost);
		if("" == host)
		{
			//地址非法
			bConnection = false;
			return bConnection;
		}
		else if (
			!mysql_real_connect(
				&m_conn, 
				host.c_str(),
				m_user.c_str(),
				m_passwork.c_str(), 
				m_dbname.c_str(), 0, NULL, 0)
			) 
		{
			//连接出错
			bConnection = false;
			return bConnection;
		}
		else
		{
			bConnection = true;
			mysql_set_character_set(&m_conn, "utf8");
			return bConnection;
		}
	}
	
	
	bool CKDatabase::Query(std::string strsql)
	{
		if(!bConnection)
		{
			return false;
		}
		int ret = mysql_query(&m_conn, strsql.c_str());
		if(0 == ret)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	CKDBSelectResult* CKDatabase::Select(std::string strsql)
	{
		if(!bConnection)
		{
			return NULL;
		}
		mysql_query(&m_conn, strsql.c_str());
		MYSQL_RES *res = mysql_store_result(&m_conn);
		if(NULL == res)
		{
			return NULL;
		}
		CKDBSelectResult *ret = new CKDBSelectResult(res);
		mysql_free_result(res);
		return ret;
	}
	
	
	void CKDatabase::FreeCKDBSR(CKDBSelectResult* free)
	{
		if(NULL != free)
		{
			delete free;
		}
	}
}
